//
//  ProductDescribeController.m
//  LearnObjectiveC
//
//  Created by 吴可高 on 16/6/13.
//  Copyright © 2016年 吴可高. All rights reserved.
//

#import "ProductDescribeController.h"
#import "ProductView.h"
#import "DescribeView.h"
#import "EvaluateView.h"

static const  CGFloat kFactor  = 1.6f;

@interface ProductDescribeController ()<UIScrollViewDelegate>
{
    NSMutableArray * _themeItems;
    CGFloat _themeItmeTitleLengh;
    UIButton * _selectedThemeItem;
    NSMutableArray * _mainScrollSubViews;
}
@property (strong, nonatomic) UIScrollView * mainScrollView;
@property (strong, nonatomic) ProductView * productView;
@property (strong, nonatomic) DescribeView * describeView;
@property (strong, nonatomic) EvaluateView * evaluateView;
@end

@implementation ProductDescribeController

#pragma mark ViewController LifeCycle
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self viewControllerInterfaceConfig];
    [self setViewControllerNavigationBarStyle];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tabBarController.tabBar setHidden:YES];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.tabBarController.tabBar setHidden:NO];
}
#pragma mark ViewController AsynRequestData
-(void)accountInfoAsynRequest;
{
    
}
#pragma mark ViewController Algorithm Processor
- (void) reverseSourceData:(id)parameter;
{
    
}
#pragma mark ViewController Initialize SubObjectView
-(void)setViewControllerNavigationBarStyle
{
    [self customNavigationBarStyleWithleftBarItems:@[@"back",@""]
                               navigationItemTitle:@"describeViewController"
                            andRightBarButtonItems:@[@"share",@""]
                         leftBarItemHandleComplete:^(NSInteger idx, UIButton *parameter) {
                             
                         }
                        rightBarItemHanbleComplete:^(NSInteger idx, UIButton *parameter) {
                            
                        }];
}

-(void)viewControllerInterfaceConfig
{
    self.view.backgroundColor = [UIColor whiteColor];
    _themeItems = [NSMutableArray array];
    [self configurationSubViewTheme];
    [self.view addSubview:self.mainScrollView];
    __block  UIButton * item = [_themeItems firstObject];
    [self.mainScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(item.mas_bottom).with.offset(10);
        make.left.mas_equalTo(self.view.mas_left).with.offset(0.f);
        make.right.mas_equalTo(self.view).with.offset(0.f);
        make.bottom.mas_equalTo(self.view.mas_bottom).with.offset(0);
    }];
    self.mainScrollView.contentOffset = CGPointMake(0, 0);
    self.mainScrollView.directionalLockEnabled  = YES;
    [self.mainScrollView addSubview:self.productView];
    [self.mainScrollView addSubview:self.describeView];
    [self.mainScrollView addSubview:self.evaluateView];
    _mainScrollSubViews = [NSMutableArray arrayWithArray:@[self.productView,self.describeView,self.evaluateView]];
    //
    [_mainScrollSubViews enumerateObjectsUsingBlock:^(UIView * subView, NSUInteger idx, BOOL * stop) {
        subView.frame =  CGRectMake(0 + kScreen_Width * idx, 0, kScreen_Width, kScreen_Height - kNavigationBarHeight*1.5);
    }];
}

-(void)configurationSubViewTheme
{
    _themeItmeTitleLengh =  0.f;
    NSArray * themes = @[@"产品",@"描述",@"评价"];
    for (int idx = 0;  idx < themes.count; idx++)
    {
        UIButton * themeItem = [UIButton buttonWithType:UIButtonTypeCustom];
        [themeItem setTitle:themes[idx] forState:UIControlStateNormal];
        [themeItem setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [themeItem setTitleColor:UIColorFromRGB(0xf8c301) forState:UIControlStateSelected];
        themeItem.tag = 1000 + idx;
        themeItem.titleLabel.font = [UIFont systemFontOfSize:14.5f];
        [themeItem addTarget:self action:@selector(thermalDidState:) forControlEvents:UIControlEventTouchUpInside];
        [themeItem.titleLabel sizeToFit];
        _themeItmeTitleLengh += themeItem.titleLabel.frame.size.width;
        [self.view addSubview:themeItem];
        [_themeItems addObject:themeItem];
        if (idx ==0)
        {
            themeItem.selected = YES;
            _selectedThemeItem  = themeItem;
        }
    }
    //
    CGFloat kThemePadding = 20.f;
    CGFloat kThemeMargin  = (kScreen_Width - _themeItmeTitleLengh  - (kThemePadding+5)* (_themeItems.count-1))/2.f;
    [_themeItems enumerateObjectsUsingBlock:^(UIButton *button, NSUInteger idx, BOOL * _Nonnull stop) {
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.view.mas_top).with.offset(10+kNavigationBarHeight);
            make.left.mas_equalTo(kThemeMargin + (_themeItmeTitleLengh /3.f + 5 + kThemePadding)*idx);
            make.width.mas_equalTo(_themeItmeTitleLengh/3.f);
        }];
    }];
}

#pragma mark --viewController getter methods-
-(UIScrollView *)mainScrollView
{
    _mainScrollView = ({
        if (!_mainScrollView) {
            _mainScrollView   = [[UIScrollView alloc]initWithFrame:CGRectZero];
            _mainScrollView.delegate = self;
            _mainScrollView.pagingEnabled = YES;
            _mainScrollView.showsHorizontalScrollIndicator = NO;
            _mainScrollView.showsVerticalScrollIndicator = NO;
            _mainScrollView.bounces = NO;
            _mainScrollView.contentSize = CGSizeMake(kScreen_Width*3 , kScreen_Height - kNavigationBarHeight*kFactor);
        }
        _mainScrollView;
    });
    return _mainScrollView;
}

-(ProductView *)productView
{
    _productView = ({
        if (!_productView) {
            _productView  = [[ProductView alloc]initWithFrame:CGRectZero];
        }
        _productView;
    });
    return _productView;
}
- (DescribeView *)describeView
{
    _describeView = ({
        if (!_describeView) {
            _describeView = [[DescribeView alloc]initWithFrame:CGRectZero];
        }
        _describeView;
    });
    return _describeView;
}
-(EvaluateView *)evaluateView
{
    _evaluateView = ({
        if (!_evaluateView) {
            _evaluateView = [[EvaluateView alloc]initWithFrame:CGRectZero];
        }
        _evaluateView;
    });
    return _evaluateView;
}
#pragma mark Possess  Delegate ViewController（委托代理者）
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ([scrollView  isMemberOfClass:[UITableView class]]) {
        
        NSLog(@"scrollView.numberOfClass = [UITableView Class]");
    }
    else
    {
        CGFloat scrollViewOffSet = scrollView.contentOffset.x;
        NSInteger idx = roundf(scrollViewOffSet/kScreen_Width);
        [self thermalDidState:_themeItems[idx]];
    }
}
#pragma mark IBOutlet Action ViewController
-(void)thermalDidState:(UIButton*)param
{
    if (param != _selectedThemeItem) {
        _selectedThemeItem.selected = NO;
        param.selected = YES;
        _selectedThemeItem = param;
        NSInteger  idx = [_themeItems indexOfObject:param];
        self.mainScrollView.contentOffset = CGPointMake(idx* kScreen_Width, 0);
    }
}

#pragma mark Other Method
-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
}
@end
